<html>

 <head><title>Click noreferrer links</title>
 <script>
  function setOriginForLinks(baseUrl) {
    var links = [
        'noref_and_tblank_link',
        'noopener_and_tblank_link',
        'tblank_link',
        'noref_link',
        'noopener_link'
    ];
    links.forEach(function(linkId) {
      link = document.getElementById(linkId);
      link.href = baseUrl + link.pathname.substr(1);
    });
  }
  function simulateClick(target) {
    var evt = document.createEvent("MouseEvents");
    evt.initMouseEvent("click", true, true, window,
                       0, 0, 0, 0, 0, false, false,
                       false, false, 0, null);

    return target.dispatchEvent(evt);
  }

  function clickNoRefTargetBlankLink() {
    return simulateClick(document.getElementById("noref_and_tblank_link"));
  }

  function clickNoOpenerTargetBlankLink() {
    return simulateClick(document.getElementById("noopener_and_tblank_link"));
  }

  function clickSameSiteNoRefTargetedLink() {
    return simulateClick(
        document.getElementById("samesite_noref_and_targeted_link"));
  }

  function clickSameSiteNoOpenerTargetedLink() {
    return simulateClick(
        document.getElementById("samesite_noopener_and_targeted_link"));
  }

  function clickSameSiteTargetedLink() {
    return simulateClick(document.getElementById("samesite_targeted_link"));
  }

  function clickSameSiteTargetBlankLink() {
    return simulateClick(document.getElementById("samesite_tblank_link"));
  }

  function clickTargetBlankLink() {
    return simulateClick(document.getElementById("tblank_link"));
  }

  function clickNoRefLink() {
    return simulateClick(document.getElementById("noref_link"));
  }

  function clickNoOpenerLink() {
    return simulateClick(document.getElementById("noopener_link"));
  }

  function clickBlankTargetedLink() {
    return simulateClick(document.getElementById("blank_targeted_link"));
  }

  function testScriptAccessToWindow() {
    // Grab a reference to the existing foo window and access its location.
    try {
      var w = window.open("", "foo");
      var url = w.location.href;
      return url != undefined;
    } catch (e) {
      return false;
    }
  }

  function testCloseWindow() {
    // Grab a reference to the existing foo window and close it.
    var w = window.open("", "foo");
    w.close();
    return true;
  }

  // Listen to incoming messages and reply to them.
  var receivedMessages = 0;
  window.addEventListener("message", messageReceived, false);
  function messageReceived(event) {
    receivedMessages++;
    event.source.postMessage(event.data, "*");
  }

  // Send a message which contains a message port.
  var mc;
  function postWithPortToFoo() {
    mc = new MessageChannel();
    mc.port1.onmessage = portMessageReceived;
    mc.port1.start();
    var w = window.open("", "foo");
    w.postMessage({message: "msg-with-port", port: mc.port2}, "*", [mc.port2]);
    return true;
  }

  var receivedMessagesViaPort = 0;
  function portMessageReceived(event) {
    receivedMessagesViaPort++;
    // Change the title to generate a notification.
    document.title = event.data;
  }

  var last_opened_window = undefined;
  function saveWindowReference() {
    // Grab a reference to the existing foo window into a global variable
    // for later testing.
    last_opened_window = window.open("", "foo");
  }
  function getLastOpenedWindowLocation() {
    return last_opened_window.location.href;
  }

  function openWindowWithTargetAndFeatures(path, target, features) {
    var w = window.open(path, target, features);
    return w !== null;
  }
 </script>
 </head>

<a href="title2.html" id="samesite_noref_and_targeted_link"
   rel="noreferrer" target="foo">
  same-site rel=noreferrer and target=foo</a><br>
<a href="title2.html" id="samesite_noopener_and_targeted_link"
   rel="noopener" target="foo">
  same-site rel=noopener and target=foo</a><br>
<a href="navigate_opener.html" id="samesite_targeted_link" target="foo">
  same-site target=foo</a><br>
<a href="title2.html" id="samesite_tblank_link" rel="opener" target="_blank">
  same-site rel=opener target=_blank</a><br>
<a href="about:blank" id="blank_targeted_link" target="foo">
  blank_targeted_link=foo</a><br>

<!-- The following set of links have to be fixed at runtime with the proper
  scheme://host:port/ string, since the port is randomly generated. The
  setOriginForLinks method is provided for facilitating the replacement and
  should be called by each browser test utilizing these links. -->
<a href="http://REPLACE/title2.html"
   id="noref_and_tblank_link" rel="noreferrer" target="_blank">
  rel=noreferrer and target=_blank</a><br>
<a href="http://REPLACE/title2.html"
   id="noopener_and_tblank_link" rel="noopener" target="_blank">
  rel=noopener and target=_blank</a><br>
<a href="http://REPLACE/title2.html" id="tblank_link"
   rel="opener" target="_blank">rel=opener target=_blank</a><br>
<a href="http://REPLACE/title2.html" id="noref_link"
   rel="noreferrer">rel=noreferrer</a><br>
<a href="http://REPLACE/title2.html" id="noopener_link"
   rel="noopener">rel=opener</a><br>


<iframe id="frame1" src="frame_tree/1-1.html"></iframe>

</html>
